package mailclient.mail;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import mailclient.object.FileObject;
import mailclient.object.Mail;
import mailclient.system.MailContext;
import util.mail.MailConfigBean;
import util.mail.SpringMail;
import util.mail.SpringMailBean;

/**
 * 邮件发送实现类，创建session、mailMessage对象， 通过getAddress获得所有的收件人地址的地址，向服务器发送邮件信息
 * 
 */
public class MailSenderImpl implements MailSender {

	@Override
	public Mail send(Mail mail, MailContext ctx) {
		try {
			Session session = ctx.getSession();
			Message mailMessage = new MimeMessage(session);
			//设置发件人地址
			Address from = new InternetAddress(ctx.getUser() + " <" + ctx.getAccount() + ">");
			System.out.println(ctx.getAccount());
			//mailMessage.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(ctx.getAccount()));
			mailMessage.setFrom(from);
			//设置所有收件人的地址
			Address[] to = getAddress(mail.getReceivers());
			mailMessage.setRecipients(Message.RecipientType.TO, to);
			//设置抄送人地址
			Address[] cc = getAddress(mail.getCcs());
			mailMessage.setRecipients(Message.RecipientType.CC, cc);
			//设置主题
			mailMessage.setSubject(mail.getSubject());
			//发送日期
			mailMessage.setSentDate(new Date());
			//构建整封邮件的容器
			Multipart main = new MimeMultipart();
			//正文的body
			BodyPart body = new MimeBodyPart();
			body.setContent(mail.getContent(), "text/html; charset=utf-8");
			main.addBodyPart(body);
			//处理附件
			for (FileObject f : mail.getFiles()) {
				//每个附件的body
				MimeBodyPart fileBody = new MimeBodyPart();
				fileBody.attachFile(f.getFile());
				//为文件名进行转码
				fileBody.setFileName(MimeUtility.encodeText(f.getSourceName()));
				main.addBodyPart(fileBody);
			}
			//将正文的Multipart对象设入Message中
			mailMessage.setContent(main);

			SpringMail springMail = new SpringMail();

			SpringMailBean smail = new SpringMailBean();
			smail.setContext(mail.getContent());
			smail.setTitle(mail.getSubject());

			String[] tolist = new String[mail.getReceivers().size()];

			for (int i = 0; i < mail.getReceivers().size(); i++) {
				tolist[i] = mail.getReceivers().get(i);
			}

			smail.setTo(tolist);
			List<String> ccs = mail.getCcs();
			if (ccs != null && ccs.size() > 0) {
				String[] ary = new String[ccs.size()];
				int index = 0;
				for (String string : ccs) {
					ary[index++] = string;
				}
				smail.setBcc(ary);
			}
			File[] files = null;
			String[] fileNames = null;
			List<FileObject> flist = mail.getFiles();
			if (flist != null && flist.size() > 0) {
				files = new File[flist.size()];
				fileNames = new String[flist.size()];
				int index = 0;
				for (FileObject f : mail.getFiles()) {
					files[index] = f.getFile();
					fileNames[index] = f.getSourceName();
					index++;
				}
			}

			MailConfigBean bean = new MailConfigBean();
			bean.setFrom(ctx.getAccount());
			bean.setHost(ctx.getSmtpHost());
			bean.setPassword(ctx.getPassword());
			bean.setUserName(ctx.getAccount());
			System.out.println(bean);
			springMail.sendMimeMail(bean, files, fileNames, smail);

			//Transport.send(mailMessage);
			return mail;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	//	@Override
	//	public Mail send(Mail mail, MailContext ctx) {
	//		try {
	//			Session session = ctx.getSession();
	//			Message mailMessage = new MimeMessage(session);
	//			//设置发件人地址
	//			Address from = new InternetAddress(ctx.getUser() + " <" + ctx.getAccount() + ">");
	//			System.out.println(ctx.getAccount());
	//			//mailMessage.addRecipients(MimeMessage.RecipientType.CC, InternetAddress.parse(ctx.getAccount()));
	//			mailMessage.setFrom(from);
	//			//设置所有收件人的地址
	//			Address[] to = getAddress(mail.getReceivers());
	//			mailMessage.setRecipients(Message.RecipientType.TO, to);
	//			//设置抄送人地址
	//			Address[] cc = getAddress(mail.getCcs());
	//			mailMessage.setRecipients(Message.RecipientType.CC, cc);
	//			//设置主题
	//			mailMessage.setSubject(mail.getSubject());
	//			//发送日期
	//			mailMessage.setSentDate(new Date());
	//			//构建整封邮件的容器
	//			Multipart main = new MimeMultipart();
	//			//正文的body
	//			BodyPart body = new MimeBodyPart();
	//			body.setContent(mail.getContent(), "text/html; charset=utf-8");
	//			main.addBodyPart(body);
	//			//处理附件
	//			for (FileObject f : mail.getFiles()) {
	//				//每个附件的body
	//				MimeBodyPart fileBody = new MimeBodyPart();
	//				fileBody.attachFile(f.getFile());
	//				//为文件名进行转码
	//				fileBody.setFileName(MimeUtility.encodeText(f.getSourceName()));
	//				main.addBodyPart(fileBody);
	//			}
	//			//将正文的Multipart对象设入Message中
	//			mailMessage.setContent(main);
	//			
	//			Transport.send(mailMessage);
	//			return mail;
	//		} catch (Exception e) {
	//			e.printStackTrace();
	//			throw new SendMailException("发送邮件错误, 请检查邮箱配置及邮件的相关信息");
	//		}
	//	}
	//获得所有的收件人地址或者抄送的地址
	private Address[] getAddress(List<String> addList) throws Exception {
		Address[] result = new Address[addList.size()];
		for (int i = 0; i < addList.size(); i++) {
			if (addList.get(i) == null || "".equals(addList.get(i))) {
				continue;
			}
			result[i] = new InternetAddress(addList.get(i));
		}
		return result;
	}
}
